package com.sinosoft.dsp.platform.components.portal.input.elements;

import java.util.Date;

import org.hibernate.Query;

import com.sinosoft.builder.templates.concept.TagElementParameterType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.platform.components.helper.TagElementHelper;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.DateUtils;

public class CalendarTagElememt implements ISinoTagElement {

	private static final Log log = Log.getLog(CalendarTagElememt.class
			.getName());

	public static final String PARAMETER_PATTERN = "pattern";

	// 日期标签
	private Date value;

	public Date getValue() {
		return value;
	}

	public void setValue(Date value) {
		this.value = value;
	}

	@Override
	public void setParameter(Query query, String key) {
		if (!isEmpty()) {
			query.setDate(key, value);
		}
	}

	@Override
	public boolean isEmpty() {
		return value == null;
	}

	@Override
	public void initValue(TagElementType type) throws SinoPlatformException {
		try {
			if (type != null) {
				TagElementParameterType param = TagElementHelper
						.getTagElementParameter(type, PARAMETER_PATTERN);
				if (!TagElementHelper.isEmptyTagElementParameter(param)) {
					this.value = DateUtils.str2dat(type.getValue(), param
							.getValue());
				} else {
					this.value = DateUtils.str2dat(type.getValue());
				}
			}
		} catch (Exception e) {
			log.error("初始化日期值出现错误。");
			throw new SinoPlatformException("初始化日期值出现错误。");
		}
	}
}
